import { createRouter, createWebHistory } from "vue-router";
import ShowSchedule from "../components/ShowSchedule.vue";
import Login from "../components/Login.vue";
import Regist from "../components/Regist.vue";
import { defineUser } from "../stores/userStore.js";
import pinia from "../pinia.js";
import Index from "../components/Index.vue"
//注意这里需要传入pinia
let sysUser = defineUser(pinia);


const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path:"/index",
      component:Index
    },
    {
      path: "/",
      component: Login,
    },
    {
      path: "/login",
      component: Login,
    },
    {
      path: "/regist",
      component: Regist,
    },
    {
      path: "/showSchedule",
      component: ShowSchedule,
    },
  ],
});
//前置路由,拦截未登录用户请求
router.beforeEach((to, from, next) => {
  //判断是否登陆过
  if (to.path == "/showSchedule") {
    console.log(sysUser.getUid)
    if (sysUser.username == "") {
      alert("您尚未登录，请登陆后再查看");
      next("/login");
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
